<?php
namespace app\common\model;

class ShopExtendModel extends BaseModel
{
    protected $name = 'shop_extend';
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    /*读取器*/
    protected function getItemListAttr($value, $data)
    {
        $itemNameList = ShopExtendItemModel::where('extend_id', $data['extend_id'])->order('item_sort')->column('item_name');
        if (empty($itemNameList)) {
            return '';
        }
        return implode(',', $itemNameList);
    }
    /*修改器*/

    /**
     * 获取扩展html
     * @param $extendId
     */
    public function getExtendHtml($extendId, $goodsId = 0)
    {
        $extendItemModel = new ShopExtendItemModel();
        $itemList        = $extendItemModel->where('extend_id', $extendId)->order('item_sort')->select();
        $html            = "<table class=\"table table-bordered\">" . PHP_EOL;
        if ($goodsId > 0) {
            $shopGoodsExtendModel = new ShopGoodsExtendModel();
            $goodsExtend          = $shopGoodsExtendModel->where('goods_id', $goodsId)->column('id,value', 'item_id');
        }
        foreach ($itemList as $v) {
            if (!empty($goodsExtend)) {
                $defaultValue = $goodsId > 0 ? $goodsExtend[$v['item_id']]['value'] : $v['item_default'];
            } else {
                $defaultValue = $v['item_default'];
            }
            $function    = '_' . $v['item_type'];
            $formControl = $this->$function($v, $defaultValue); //当前表单
            $html .= "<tr>" . PHP_EOL;
            $html .= "<th>" . PHP_EOL;
            if ($goodsId > 0) {
                $html .= "<input type=\"hidden\" name=\"extend[{$v['item_id']}][id]\" value=\"{$goodsExtend[$v['item_id']]['id']}\" />" . PHP_EOL;
            }
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_id']}][extend_id]\" value=\"{$v['extend_id']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_id']}][item_id]\" value=\"{$v['item_id']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_id']}][item_name]\" value=\"{$v['item_name']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_id']}][is_search]\" value=\"{$v['is_search']}\" />" . PHP_EOL;
            $html .= "<input type=\"hidden\" name=\"extend[{$v['item_id']}][item_sort]\" value=\"{$v['item_sort']}\" />" . PHP_EOL;
            $html .= "{$v['item_name']}" . PHP_EOL;
            $html .= "</th>" . PHP_EOL;
            $html .= "<td>{$formControl}</td>" . PHP_EOL;
            $html .= "</tr>" . PHP_EOL;
        }
        $html .= "</table>" . PHP_EOL;
        return $html;
    }

    /**
     * 文本框
     */
    private function _text($itemInfo, $defaultValue)
    {
        $html = "<input type=\"text\" class=\"form-control\" name=\"extend[{$itemInfo['item_id']}][value]\" value=\"{$defaultValue}\">" . PHP_EOL;
        return $html;
    }

    /**
     * 下拉框
     */
    private function _select($itemInfo, $defaultValue)
    {
        $options = '';
        if (!empty($itemInfo['item_value'])) {
            $values = explode(',', $itemInfo['item_value']);
            foreach ($values as $value) {
                $selected = $value == $defaultValue ? 'selected' : '';
                $options .= "<option value=\"{$value}\" $selected>{$value}</option>" . PHP_EOL;
            }
        }
        $html = "<select class=\"form-control\" name=\"extend[{$itemInfo['item_id']}][value]\">" . PHP_EOL;
        $html .= $options . PHP_EOL;
        $html .= "</select>" . PHP_EOL;
        return $html;
    }

    /**
     * 单选框
     */
    private function _radio($itemInfo, $defaultValue)
    {
        $html = '';
        if (!empty($itemInfo['item_value'])) {
            $values = explode(',', $itemInfo['item_value']);
            foreach ($values as $value) {
                $checked = $value == $defaultValue ? 'checked' : '';
                $html .= "<label class=\"radio-inline i-checks\">" . PHP_EOL;
                $html .= "<input type=\"radio\" name=\"extend[{$itemInfo['item_id']}][value]\" value=\"{$value}\" {$checked} /> {$value}" . PHP_EOL;
                $html .= "</label>" . PHP_EOL;
            }
        }
        return $html;
    }

    /**
     * 多选框
     */
    private function _checkbox($itemInfo, $defaultValue)
    {
        $html = '';
        if (!empty($itemInfo['item_value'])) {
            $values = explode(',', $itemInfo['item_value']);
            foreach ($values as $value) {
                if (empty($defaultValue)) {
                    $checked = '';
                } else {
                    $checked = in_array($value, explode(',', $defaultValue)) ? 'checked' : '';
                }
                $html .= "<label class=\"checkbox-inline i-checks\">" . PHP_EOL;
                $html .= "<input type=\"checkbox\" name=\"extend[{$itemInfo['item_id']}][value][]\" value=\"{$value}\" {$checked} /> {$value}" . PHP_EOL;
                $html .= "</label>" . PHP_EOL;
            }
        }
        return $html;
    }
}
